﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace HelperPrototype.TabPageControls
{
    public partial class QuanLyPhongBan : UserControl
    {
        public QuanLyPhongBan()
        {
            InitializeComponent();
        }

        private void QuanLyPhongBan_Load(object sender, EventArgs e)
        {
            loadData();
        }

        private void loadData()
        {
            _DB.DataClasses1DataContext dc = new _DB.DataClasses1DataContext();
            var listPhongBan = from pb in dc.PhongBans
                               where pb.deleted == false
                               select pb;

            dgvPhongBan.Rows.Clear();
            int count = 1;
            foreach (_DB.PhongBan pb in listPhongBan)
            {
                DataGridViewRow r = new DataGridViewRow();
                r.CreateCells(dgvPhongBan);
                r.Cells[0].Value = count++;
                r.Cells[1].Value = pb.MaPhongBan;
                r.Cells[2].Value = pb.TenPhongBan;
                r.Cells[3].Value = pb.NguoiDungs.Count;

                dgvPhongBan.Rows.Add(r);
            }
        }

        private void button4_Click(object sender, EventArgs e)
        {
            OtherForms.ThemPhongBan f = new OtherForms.ThemPhongBan();
            f.ShowDialog();
            loadData();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (dgvPhongBan.SelectedRows.Count <= 0)
                return;

            string maPhongBan = (string)dgvPhongBan.SelectedRows[0].Cells[1].Value;
            OtherForms.ThemPhongBan f = new OtherForms.ThemPhongBan(maPhongBan);
            f.ShowDialog();
            loadData();
        }

        private void button6_Click(object sender, EventArgs e)
        {
            // check
            if (dgvPhongBan.SelectedRows.Count == 0)
                return;

             if (MessageBox.Show("Chắc chắn xóa?", "Xác nhận", MessageBoxButtons.YesNo) == DialogResult.No)
                 return;

            // go
            _DB.DataClasses1DataContext dc = new _DB.DataClasses1DataContext();
            foreach (DataGridViewRow row in dgvPhongBan.SelectedRows)
            {
                String ID = (String)row.Cells[1].Value;
                string maPhongBan = (string)row.Cells[1].Value;
                _DB.PhongBan pb = dc.PhongBans.Single(x => x.MaPhongBan == maPhongBan);
                if (pb == null)
                return;

                pb.deleted = true;
            }

            dc.SubmitChanges();
            loadData();
        }




    }
}
